package com.cmict.data.system.api.dept;

import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.util.collection.CollectionUtils;
import com.cmict.data.system.api.dept.dto.DeptRespDTO;
import com.cmict.data.system.enums.ApiConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

@FeignClient(name = ApiConstants.NAME) // TODO awesome：fallbackFactory =
@Api(tags = "RPC 服务 - 部门")
public interface DeptApi {

    String PREFIX = ApiConstants.PREFIX + "/dept";

    @GetMapping(PREFIX + "/get")
    @ApiOperation("获得部门信息")
    @ApiImplicitParam(name = "id", value = "部门编号", example = "1024", required = true, dataTypeClass = Long.class)
    CommonResult<DeptRespDTO> getDept(@RequestParam("id") Long id);

    @GetMapping(PREFIX + "/getDeptsByParentId")
    @ApiOperation("获得该部门下所有子部门信息")
    @ApiImplicitParam(name = "id", value = "部门编号", example = "1024", required = true, dataTypeClass = Long.class)
    CommonResult<List<DeptRespDTO>> getDeptsByParentId(@RequestParam("id") Long id);

    @GetMapping(PREFIX + "/getAllId")
    @ApiOperation("获得所有部门id")
    CommonResult<List<Long>> getAllId();

    @GetMapping(PREFIX + "/list")
    @ApiOperation("获得部门信息数组")
    @ApiImplicitParam(name = "ids", value = "部门编号数组", example = "1,2", required = true, allowMultiple = true)
    CommonResult<List<DeptRespDTO>> getDepts(@RequestParam("ids") Collection<Long> ids);

    @GetMapping(PREFIX + "/list-by-ids-join")
    @ApiOperation("获得部门信息数组")
    @ApiImplicitParam(name = "ids", value = "部门编号数组", example = "1,2", required = true, allowMultiple = true)
    CommonResult<List<DeptRespDTO>>  getDeptsByIdsJoin(@RequestParam("ids") String ids);

    @GetMapping(PREFIX + "/valid")
    @ApiOperation("校验部门是否合法")
    @ApiImplicitParam(name = "ids", value = "部门编号数组", example = "1,2", required = true, allowMultiple = true)
    CommonResult<Boolean> validDepts(@RequestParam("ids") Collection<Long> ids);

    @GetMapping(PREFIX + "/getDeptByName")
    @ApiOperation("获得部门信息")
    @ApiImplicitParam(name = "name", value = "部门编号", example = "1024", required = true, dataTypeClass = String.class)
    CommonResult<DeptRespDTO> getDeptByName(@RequestParam("name") String id);

    /**
     * 获得指定编号的部门 Map
     *
     * @param ids 部门编号数组
     * @return 部门 Map
     */
    default Map<Long, DeptRespDTO> getDeptMap(Set<Long> ids) {
        return CollectionUtils.convertMap(getDepts(ids).getCheckedData(), DeptRespDTO::getId);
    }

    default Map<Long, DeptRespDTO> getDeptMapByListIds(List<Long> ids) {
        String idList = StringUtils.join(ids, ",");
        return CollectionUtils.convertMap(getDeptsByIdsJoin(idList).getCheckedData(), DeptRespDTO::getId);
    }
    default Map<Long, DeptRespDTO> getDeptMapBySetIds(Set<Long> ids) {
        String idList = StringUtils.join(ids, ",");
        return CollectionUtils.convertMap(getDeptsByIdsJoin(idList).getCheckedData(), DeptRespDTO::getId);
    }

    @GetMapping(PREFIX + "/getDeptsByIds")
    @ApiOperation("校验部门是否合法")
    @ApiImplicitParam(name = "ids", value = "部门编号数组", example = "1,2", required = true, allowMultiple = true)
    CommonResult<List<DeptRespDTO>> getDeptsByIds(@RequestParam("ids") Collection<Long> ids);

    /**
     * 所有部门列表
     *
     * @return 部门列表
     */
    @GetMapping(PREFIX + "/all")
    @ApiOperation("校验部门是否合法")
    CommonResult<List<DeptRespDTO>> selectListAll();

    /**
     * 所有部门列表
     *
     * @return 部门列表放开的
     */
    @GetMapping(PREFIX + "/allOpen")
    @ApiOperation("查询部门状态开放")
    CommonResult<List<DeptRespDTO>> selectListAllOpen();

}
